package org.hk.proxy.server.constant;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.core.util.StrUtil;
import lombok.Data;

import java.io.IOException;
import java.io.Serializable;
import java.util.Properties;

@Data
public class ProjectConfig {
    private Integer port;
    private WebServerConfig webServerConfig;
    private static Properties properties = new Properties();
    static {
        try {
            properties.load(new ClassPathResource("application.properties").getStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static final ProjectConfig config = new ProjectConfig();
    private ProjectConfig(){
        this.webServerConfig = new WebServerConfig();
        this.port=Integer.valueOf(properties.getProperty("project.server.port"));
        this.webServerConfig.ip=properties.getProperty("project.web.server.ip");
        this.webServerConfig.port=Integer.valueOf(properties.getProperty("project.web.server.port"));
        String webPath = StrUtil.cleanBlank(String.valueOf(properties.getProperty("project.web.server.web_path")));
        if(webPath.contains("classpath")){
            String projectPath =  this.getClass().getClassLoader().getResource("").getPath().replace("/target/classes","");
            String path = projectPath+StrUtil.cleanBlank(webPath).replaceAll("classpath:","");
            this.webServerConfig.webPath=path;
        }else {
            this.webServerConfig.webPath=webPath;
        }
    }
    @Data
    public static final class WebServerConfig implements Serializable {
        private String webPath;
        private Integer port;
        private String ip;
    }
}
